/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSWindow.h"
#include "main.h"

IMPLEMENT_DYNAMIC_CLASS(NSWindow, NSResponder)

NSWindow::NSWindow() : NSResponder(), _contentView(NULL)
{
	_wv = new NSWindowView(this);
	exposeBinding(wxT("title"));
}

void NSWindow::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	NSUNUSED(archiver);
	wxASSERT(0);
}

unsigned long NSStyle2wxStyle(unsigned long style)
{
	static long styles[] = {
		wxCAPTION|wxSYSTEM_MENU,
		wxCAPTION|wxCLOSE_BOX|wxSYSTEM_MENU,
		wxCAPTION|wxMINIMIZE_BOX|wxSYSTEM_MENU,
		wxRESIZE_BORDER};
	unsigned long outstyle = 0;
	for(size_t i=0;style!= 0 && i<sizeof(styles)/sizeof(styles[0]);style>>=1, i++)
	{
		if (style & 1)
			outstyle |= styles[i];
	}
	return outstyle;
}

class MySizeEvt : public wxEvtHandler
{
public:
	MySizeEvt(NSWindow* wnd) : wxEvtHandler(), _wnd(wnd){}
	void OnSize(wxSizeEvent& evt)
	{
		NSUNUSED(evt);
		if (_wnd->windowView())
			_wnd->windowView()->setSize(_wnd->_widget()->GetClientSize());
	}
private:
	NSWindow* _wnd;
};

void NSWindow::initWithRect(wxRect *contentRect, long aStyle, bool defer)
{
	NSUNUSED(defer);
	_window = new wxFrame(NULL, wxID_ANY, wxT("my frame"), contentRect->GetPosition(),wxDefaultSize, NSStyle2wxStyle(aStyle));
	wxASSERT(_window);
	_window->Connect(wxID_ANY, wxID_ANY, wxEVT_SIZE, wxSizeEventHandler(MySizeEvt::OnSize), NULL, new MySizeEvt(this));
	_window->SetClientSize(contentRect->GetSize());
//	_window->SetBackgroundStyle(wxBG_STYLE_SYSTEM);

	_wv->_allocWidget();
	_window->Show(true);

	//	if (wxGetApp().GetTopWindow()==NULL) wxGetApp().SetTopWindow(_window);
	NSRegisterTopLevelWindow(this);
}

void NSWindow::setContentView(NSView *view)
{
	if (_contentView)
		_contentView->removeFromSuperview();
	_contentView = view;
	_wv->setContentView(_contentView);
}

void NSWindow::orderFront(void)
{
}

void NSWindow::setTitle(const wxString& title)
{
	wxVariant v(title);
	setValue(wxT("title"), &v);
}

void NSWindow::_setValue(const wxString& key, wxObject *value)
{
	if (key == wxT("title"))
	{
		wxVariant *v = wxDynamicCast(value, wxVariant);
		wxASSERT(v);
		_title = v->GetString();
		if (_window)
			_window->SetLabel(_title);
	} else if (key == wxT("initialFirstResponder"))
	{
	} else
		NSResponder::_setValue(key, value);
}

wxObject* NSWindow::_getValue(const wxString& key)
{
	if (key == wxT("title"))
	{
		return new wxVariant(_title);
	} else
		return NSResponder::_getValue(key);
}

void NSWindow::_callAction(const wxString& action, wxObject* arg, NSObject* caller)
{
	if (action == wxT("performClose:"))
	{
		if (_window)
		{
			_window->Destroy();
		}
	} else
		NSResponder::_callAction(action, arg, caller);
}
